/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-07-29 15:59:42
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-08-10 17:36:30
 * @FilePath: \KEIL-ARMd:\Users\HXD\Desktop\拓基\面板\JS32T031_SDK\User\board\driver_board_uart.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "board/driver_board_uart.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
// 驱动板串口  GPIO
#define DRIVER_BOARD_UART UART1
#define DRIVER_BOARD_UART_IRQn UART1_IRQn

#define DRIVER_BOARD_UART_Px PA
#define DRIVER_BOARD_UART_RX_PINx PIN_0
#define DRIVER_BOARD_UART_TX_PINx PIN_1
#define DRIVER_BOARD_UART_GPIO_MODE LL_GPIO_MD_AF_PP
#define DRIVER_BOARD_UART_GPIO_AF LL_GPIO_AF_1

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 rx_buf[UART_BUF_LEN] = {0};
u8 rx_index = 0;
u8 pre_tx_len = 0;
bool rx_finish = false;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
bool rx_finish_get(void)
{
    return rx_finish;
}
void rx_finish_clr(void)
{
    rx_finish = false;
}
u8 rx_buf_get(u8 *data_str)
{
    // printf("0x%x ", rx_buf[0]);
    // data_str = &rx_buf[0];
    memcpy(data_str, rx_buf, pre_tx_len);
    return pre_tx_len;
}
void driver_board_uart_init(void)
{
    //    printf("\r\n driver_board_uart_init");
    ll_gpio_config(DRIVER_BOARD_UART_Px, DRIVER_BOARD_UART_RX_PINx | DRIVER_BOARD_UART_TX_PINx, DRIVER_BOARD_UART_GPIO_MODE);
    ll_gpio_af_sel(DRIVER_BOARD_UART_Px, DRIVER_BOARD_UART_RX_PINx | DRIVER_BOARD_UART_TX_PINx, DRIVER_BOARD_UART_GPIO_AF);

    ll_uart_config(DRIVER_BOARD_UART, 19200, LL_UART_PARITY_NO); // 无校验位
    ll_uart_config(DRIVER_BOARD_UART, 56000, LL_UART_PARITY_NO);  // 无校验位
    // ll_uart_tx_complete_interrupt_enable(DRIVER_BOARD_UART);      // 使能发送完成中断
    ll_uart_rx_buf_not_empty_interrupt_enable(DRIVER_BOARD_UART); // 使能接收缓冲不为空中断
    ll_uart_rx_timeout_interrupt_enable(DRIVER_BOARD_UART);       // 使能接收超时中断
    ll_uart_rx_timeout_enable(DRIVER_BOARD_UART);                 // 接收超时检测使能
    ll_uart_enable(DRIVER_BOARD_UART);

    NVIC_EnableIRQ(DRIVER_BOARD_UART_IRQn);
}

void uart_rx_irq_handle(void)
{
    volatile u16 rx_data = 0;
    volatile u8 loop = 0;
    volatile u8 rx_cnt = 0;
    volatile u8 rx_parity_err = 0;

    if (LL_UART_RX_BUF_NOT_EMPTY_PENDING_GET(DRIVER_BOARD_UART)) // 获取接收缓冲区是否为空pending
    {
        rx_cnt = LL_UART_RX_CNT_GET(DRIVER_BOARD_UART); // 获取接收数据的数量
        for (loop = 0; loop < rx_cnt; loop++)
        {
            /* parity err bit map, bit 1 is parity err */
            rx_parity_err = LL_UART_RX_PERR_PENDING_GET(DRIVER_BOARD_UART); // 奇偶校验标志位
            if (true == ll_uart_rx(DRIVER_BOARD_UART, (u16 *)&rx_data))
            {
                if (0 == ((rx_parity_err >> 0) & 0x01))
                {
                    rx_buf[rx_index++] = rx_data;
                    rx_index %= UART_BUF_LEN;
                }
            }
        }
        LL_UART_RX_BUF_NOT_EMPTY_PENDING_CLR(DRIVER_BOARD_UART); // 清除接收缓冲区为空pending
    }
    if (LL_UART_RX_TIMEOUT_PENDING_GET(DRIVER_BOARD_UART)) // 获取接收超时pending
    {
        //        printf("rx_irq\r\n ");
        rx_finish = true;
        pre_tx_len = rx_index;
        rx_index = 0;
        LL_UART_RX_TIMEOUT_PENDING_CLR(DRIVER_BOARD_UART); // 清除接收超时pending
    }
}

/*
 * 串口发送
 */
void driver_board_uart_send(unsigned char *pBuff, unsigned int len)
{
    // tx_index = 0;
    for (unsigned int i = 0; i < len; i++)
    {
        //		printf("%u",i);
        ll_uart_tx(DRIVER_BOARD_UART, pBuff[i]);
    }
}
